package com.example.forum.config;

import com.example.forum.model.User;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class AppConfig {
    public static final String SESSION_KEY="SESSION_KEY";

    public static void setUser(HttpServletRequest request,User user){
        HttpSession session= request.getSession(true);
        session.setAttribute(SESSION_KEY,user);
    }

    public static User getUser(HttpServletRequest request){
        HttpSession session=request.getSession(false);
        if(session!=null&&session.getAttribute(SESSION_KEY)!=null){
            return (User) session.getAttribute(SESSION_KEY);
        }
        return null;
    }

    public static void removeUser(HttpServletRequest request){
        HttpSession session=request.getSession(false);
        if(session!=null){
            session.invalidate();
        }
    }
}
